package com.sky.controller.admin;

import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersConfirmDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.dto.PageDTO;
import com.sky.entity.Orders;
import com.sky.query.OrderQuery;
import com.sky.result.Result;
import com.sky.service.OrdersService;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/order")
@Api(tags = "管理端订单相关接口")
@Slf4j
public class OrderController {
    @Autowired
    OrdersService ordersService;

    @GetMapping("/conditionSearch")
    @ApiOperation("订单搜索")
    public Result<PageDTO<OrderVO>> conditionSearch(OrderQuery query){
        log.info("订单搜索:{}",query);
        PageDTO<OrderVO> pageDTO = ordersService.conditionSearch(query);
        return Result.success(pageDTO);
    }

    @GetMapping("/statistics")
    @ApiOperation("各个状态的订单数量统计")
    public Result<OrderStatisticsVO> countStatus(){
        log.info("统计各个状态的订单数量");
        OrderStatisticsVO orderStatisticsVO = ordersService.countStatus();
        return Result.success(orderStatisticsVO);
    }

    @PutMapping("/confirm")
    @ApiOperation("接单接口")
    public Result confirmOrder(@RequestBody OrdersConfirmDTO orders){
        log.info("接单：{}",orders);
        ordersService.confirmOrder(orders);
        return Result.success();
    }

    @PutMapping("/delivery/{id}")
    @ApiOperation("派送订单接口")
    public Result deliveryOrder(@PathVariable Long id){
        log.info("派送订单：{}",id);
        ordersService.deliveryOrder(id);
        return Result.success();
    }

    @PutMapping("/complete/{id}")
    @ApiOperation("完成订单接口")
    public Result completeOrder(@PathVariable Long id){
        log.info("完成订单：{}",id);
        ordersService.completeOrder(id);
        return Result.success();
    }

    @PutMapping("/cancel")
    @ApiOperation("取消订单接口")
    public Result cancelOrder(@RequestBody OrdersCancelDTO cancelDTO){
        log.info("取消订单的原因：{}",cancelDTO.getCancelReason());
        ordersService.cancelOrderAdmin(cancelDTO);
        return Result.success();
    }

    @PutMapping("/rejection")
    @ApiOperation("拒单接口")
    public Result rejectOrder(@RequestBody OrdersRejectionDTO rejectionDTO){
        log.info("拒单原因：{}",rejectionDTO.getRejectionReason());
        ordersService.rejectOrder(rejectionDTO);
        return Result.success();
    }

    @GetMapping("/details/{id}")
    @ApiOperation("查询订单详情接口")
    public Result<OrderVO> queryOrderDetails(@PathVariable Long id){
        log.info("查询订单详情：{}",id);
        OrderVO orderVO = ordersService.queryOrderDetails(id);
        return Result.success(orderVO);
    }
}
